import { PipeTransform, Pipe } from "@angular/core";
import { element } from "protractor";
import { EnumElement, EnumList } from "./enumData";
export { EnumElement } from "./enumData";
@Pipe({
    name: 'freeenum',
    pure: true
})

/** FreeEnumPipe is a pure pipe, which means the transform() method is invoked only when its input arguments change.
 *  If the pipe has internal state (that is, the result depends on state other than its arguments) changed, transform() will not be invoked. */
export class FreeEnumPipe implements PipeTransform {

    public enumList: any;

    /** params: [ array: enumList | enumType: enumListCode ];
     * if array is not undefined, it will be used first.
    */

    transform(key: string, arg: any): string {
        let a;
        if (arg && arg instanceof Array && arg.length > 0) {
            this.enumList = arg;
        } else if (arg && typeof arg === 'string' && arg != '') {
            this.enumList = this.getEnumListByCode(arg);
        }
        if (this.enumList != undefined && this.enumList.length > 0) {
            a = this.getEnumValueByKey(key);
        } else {
            a = key;
        }
        return a;
    }

    /* transform(key: string, array?: Array<EnumElement>, enumType?: string): string {//最好是any类型
        let a;
        if (array && array.length > 0) {
            this.enumList = array;
        } else if (enumType) {
            this.enumList = this.getEnumListByCode(enumType);
        }
        if (this.enumList != undefined && this.enumList.length > 0) {
            a = this.getEnumValueByKey(key);
        } else {
            a = key;
        }
        return a;
    } */

    /* transform(key: string, enumType: string): string {
        let a;
        this.enumList = this.getEnumListByCode(enumType);
        if (this.enumList != undefined && this.enumList != []) {
            a = this.getEnumValueByKey(key);
        } else {
            a = key;
        }
        return a;
    } */

    /** 不能在foreach中使用break、return，所以当前返回最后一个匹配内容。
     * 如果要返回第一个，可以使用some()方法，return true 跳出，或者every()方法，return false 跳出。
     */

    /** find enum text by key, if there are multiple result, return last.  */
    getEnumValueByKey(key): string {
        let result;
        this.enumList.forEach(element => {
            if (element.value == key) {
                result = element.text;
            }
        });
        if (result)
            return result;
        else
            return key;
    }

    /** find enum key by text, if there are multiple result, return last.  */
    getEnumKeyByValue(text): any {
        let result;
        this.enumList.forEach(element => {
            if (element.text == text) {
                result = element.value;
            }
        });
        if (result)
            return result;
        else
            return text;
    }

    /** find enumList by enumType, if there are multiple result, return last.*/
    getEnumListByCode(enumType: string): any {
        let valuesList;
        EnumList.forEach(element => {
            if (element.code == enumType) {
                valuesList = element.enums;
            }
        })
        if (valuesList)
            return valuesList;
        else
            return [];
    }

    /** find enum text by key and enum, if there are multiple result, return last.  */
    getThisEnumValueByKey(key: any, enumList: Array<EnumElement>): string {
        let result;
        enumList.forEach(element => {
            if (element.value == key) {
                result = element.text;
            }
        });
        if (result)
            return result;
        else
            return key;
    }

    /** find enum key by text and enum, if there are multiple result, return last.  */
    getThisEnumKeyByValue(text, enumList: Array<EnumElement>): string {
        let result;
        enumList.forEach(element => {
            if (element.text == text) {
                result = element.value;
            }
        });
        if (result)
            return result;
        else
            return text;
    }

}


